package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.wingroom.config.WingRoomFurnitureConfig;
import com.yanqu.road.entity.wingroom.config.WingRoomLvConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.*;

/**
 * @author ：zfr
 * @date ：2022/2/14 18:33
 */
public class WingRoomConfigDaoImpl extends TempDao {
    public Map<Integer, WingRoomFurnitureConfig> getFurnitureConfigMap() {
        Map<Integer, WingRoomFurnitureConfig> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_wingroom_furniture";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomFurnitureConfig.WingRoomFurnitureConfigBuilder builder
                        = WingRoomFurnitureConfig.WingRoomFurnitureConfigBuilder.aWingRoomFurnitureConfig();

                builder.withFurnitureId(rs.getInt("id"))
                        .withQuality(rs.getInt("quality"))
                        .withSuitType(rs.getInt("suitType"))
                        .withPlaceType(rs.getInt("placeType"))
                        .withRoomType(rs.getInt("roomType"))
                        .withUpgradeType(rs.getInt("upgradeType"))
                        .withPlaceNum(rs.getInt("placeNum"))
                        .withPrice(rs.getString("price"))
                        .withGeomancy(rs.getInt("geomancy"))
                        .withIncreaseGeomancy(rs.getInt("increaseGeomancy"));

                //几个参数额外处理
                String placeArea = rs.getString("placeArea");
                if (placeArea.equals("0")) {
                    builder.withIsFloor(true);  //地板
                } else {
                    String[] placeAreaPar = placeArea.split("\\|");
                    builder.withXLen(Integer.parseInt(placeAreaPar[0]));
                    builder.withYLen(Integer.parseInt(placeAreaPar[1]));
                }
                String skills = rs.getString("skills");
                List<Integer> list = StringUtils.stringToIntegerList(skills, "\\|");
                builder.withSkills(list);

                WingRoomFurnitureConfig config = builder.build();

                dataMap.put(config.getFurnitureId(), config);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, WingRoomLvConfig> getWingRoomLvConfigList() {
        Map<Integer, WingRoomLvConfig> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_badge where `type` = 1";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomLvConfig.WingRoomLvConfigBuilder builder = WingRoomLvConfig.WingRoomLvConfigBuilder.aWingRoomLvConfig();
                builder.withId(rs.getInt("id"))
                        .withLv(rs.getInt("level"))
                        .withNeedComfort(rs.getLong("unlockValue"))
                        .withSkillList(StringUtils.stringToIntegerList(rs.getString("skillsList"), "\\|"))
                        .withUnlockRegion(StringUtils.stringToIntegerList(rs.getString("specialEffect"), "\\|"));
                WingRoomLvConfig wingRoomLvConfig = builder.build();
                map.put(wingRoomLvConfig.getLv(), wingRoomLvConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
